package com.iflytek.edu.db.agent.config;

import com.alibaba.druid.DbType;
import com.iflytek.edu.db.core.enums.AdapterType;
import lombok.Data;

/**
 * 参数配置
 *
 * @author linkai
 * @date 2022/2/18
 */
@Data
public final class ArgsConfig {
    public final static ArgsConfig INSTANCE = new ArgsConfig();

    private ArgsConfig() {
    }

    private DbType dbType = DbType.dm;
    private AdapterType adapterType = AdapterType.adapter;

    /**
     * 参数解析
     *
     * @param args 配置参数
     */
    public static void parse(String args) {
        String[] split = args.split(",");
        if (split.length == 1) {
            // 获取 数据类型
            String dbTypeName = split[0];
            try {
                INSTANCE.dbType = DbType.valueOf(dbTypeName);
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("db-adapter dbType error! daType = " + dbTypeName, e);
            }
        } else if (split.length > 1) {
            // 获取 数据类型
            String dbTypeName = split[0];
            try {
                INSTANCE.dbType = DbType.valueOf(dbTypeName);
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("db-adapter dbType error! daType = " + dbTypeName, e);
            }
            // 获取 适配类型
            String agentTypeName = split[1];
            try {
                INSTANCE.adapterType = AdapterType.valueOf(agentTypeName);
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("db-adapter agentType error! agentType = " + dbTypeName, e);
            }
        }
    }
}
